package com.example.ljw.frescowallpaper.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.example.ljw.frescowallpaper.R;
import com.example.ljw.frescowallpaper.bean.UserBean;
import com.example.ljw.frescowallpaper.config.Constants;
import com.example.ljw.frescowallpaper.utils.PreUtils;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class SplashActivity extends Activity {
    private Context mContext = this;
    private ImageView image_splash;
    private Animation splash_alphaAnim;
    private String userName;
    private static final long SLEEP_TIME = 2000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //隐藏状态栏
        //定义全屏参数
        int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        //获得当前窗体对象
        Window window = SplashActivity.this.getWindow();
        //设置当前窗体为全屏显示
        window.setFlags(flag, flag);
        setContentView(R.layout.activity_splash);
        
        initView();

        new LoadTask().execute();
    }

    private void initView() {
        image_splash = (ImageView) findViewById(R.id.image_splash);
        //闪屏动画效果
        splash_alphaAnim = AnimationUtils.loadAnimation(mContext, R.anim.splash_alpha);
        image_splash.startAnimation(splash_alphaAnim);
    }

    class LoadTask extends AsyncTask<Void,Void,Void>{
        @Override
        protected Void doInBackground(Void... voids) {
            //获得起始时间
            long startTime = System.currentTimeMillis();
            //网络加载的操作
            String s = PreUtils.readStrting(mContext, Constants.KEY_OF_OBJECTID);
            if (s.isEmpty()){
                PreUtils.writeString(SplashActivity.this, Constants.KEY_OF_OBJECTID,"8BkXsLLs");
            }

            //获得结束时间
            long endTime = System.currentTimeMillis();
            long loadTime = endTime - startTime;
            if (loadTime<SLEEP_TIME){
                try {
                    Thread.sleep(SLEEP_TIME-loadTime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
//            userName = PreUtils.readStrting(SplashActivity.this, Constants.KEY_HAVA_USER);
            Intent intent = new Intent();

            intent.setClass(mContext,NavigationActivity.class);
            startActivity(intent);
        }
    }


}
